package com.farm.configer.form.impl;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.web.servlet.ModelAndView;

import com.farm.configer.domain.Configer;
import com.farm.configer.domain.Configerform;
import com.farm.configer.domain.ex.ConfigerMetaPara;
import com.farm.configer.form.FormModel;
import com.farm.configer.form.FormModelUtils;
import com.farm.core.page.ViewMode;

/**
 * 自定义模型(通过用户在界面手动录入)
 * 
 * @author Wd
 *
 */
public class FieldCompleteConfigerModelImpl extends FormModelUtils implements FormModel {
	private Configer configer;

	public FieldCompleteConfigerModelImpl(Configer configer) {
		this.configer = configer;
	}

	@Override
	public String getFormPageUrl() {
		return "confweb/baseForm.do?configerid=" + configer.getId();
	}

	@Override
	public String getFormPageSubmitUrl() {
		return "confweb/baseSubmitForm.do?configerid=" + configer.getId();
	}

	@Override
	public ModelAndView getFormPageModelAndView(List<ConfigerMetaPara> testParaList,Object jspConfiger) {
		Map<String, Object> configerMap = new HashMap<String, Object>();
		for (ConfigerMetaPara node : testParaList) {
			configerMap.put(node.getCode(), jsonStrToObjects(node.getJsonString()));
		}
		return ViewMode.getInstance().putAttr("config", configerMap).putAttr("attrs", jspConfiger)
				.returnModelAndView("web-configer-forms/commons/fieldComplete");
	}

	

	@Override
	public Map<String, Object> submitForm(List<ConfigerMetaPara> paraList) {
		Map<String, Object> configerMap = new HashMap<String, Object>();
		List<Map<String, String>> fields = new ArrayList<Map<String, String>>();
		{
			Map<String, Map<String, String>> dic = new HashMap<String, Map<String, String>>();
			for (ConfigerMetaPara node : paraList) {
				// ID_code
				String[] id_codes = node.getCode().split("_#_");
				if (id_codes.length == 2) {
					String id = id_codes[0];
					String name = id_codes[1];
					String val = node.getJsonString();

					Map<String, String> fieldObj = dic.get(id);
					if (fieldObj == null) {
						fieldObj = new HashMap<String, String>();
						dic.put(id, fieldObj);
					}
					fieldObj.put(name, val);
				}
			}
			for (Map<String, String> node : dic.values()) {
				fields.add(node);
			}
			fields.sort(new Comparator<Map<String, String>>() {
				@Override
				public int compare(Map<String, String> o1, Map<String, String> o2) {
					String sort1 = o1.get("no2no");
					String sort2 = o2.get("no2no");
					return Integer.valueOf(sort1) - Integer.valueOf(sort2);
				}
			});
		}
		Map<String, Object> val = new LinkedHashMap<String, Object>();
		val.put("list", fields);
		configerMap.put(configer.getCode(), val);
		return configerMap;
	}

	@Override
	public List<Configerform> getForms() {
		List<Configerform> list = new ArrayList<Configerform>();
		Configerform sqlInput = new Configerform();
		sqlInput.setName("单表信息");
		sqlInput.setCode("tables");
		sqlInput.setModel("inner");
		sqlInput.setInnercode("none");
		sqlInput.setValid("none");
		sqlInput.setSort(1);
		sqlInput.setPcontent(
				"单表信息，如可以填写“单表SQL”模型的输出值，如{ 'type' : 'mysql','table' : {'name' : 'farm_code_field','fields' : [ ...]}}");
		sqlInput.setConfigerid(configer.getId());
		list.add(sqlInput);
		return list;
	}

}
